﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfSnqkGasAnalysis.IBusiness_impl;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis
{
    /// <summary>
    /// 水露点，审核
    /// </summary>
    /// 创建时间：2022-12-26 10:17:51。作者：王浩力
    public partial class WindowWaterpointCheck : Window
    {
        public WindowWaterpointCheck()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 被审核的数据
        /// </summary>
        internal Water_point editInfo;

        private long waterpointKEY = 0;
        WaterPointBusiness waterpointBusiness = new WaterPointBusiness();
        SourceDictBusiness sourceDictBusiness = new SourceDictBusiness();
        UserBusiness userBusiness = new UserBusiness();

        /// <summary>
        /// DataGrid 分页数据刷新
        /// </summary>
        internal event EventHandler DataGridRefresh;

        //驳回
        private void Reject_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.txtReject.Text))
            {
                ShowAlertHelpter.ShowAlert("驳回理由不可为空");
                e.Handled = true;
                return;
            }
            if (this.txtReject.Text.Trim().Equals("驳回理由"))
            {
                e.Handled = true;
                return;
            }

            Water_check_history history = new Water_check_history();
            history.WaterPointId = waterpointKEY;
            history.Check_stat = 0;
            history.Reject_reason = txtReject.Text;
            var result = waterpointBusiness.Check(history);
            if (result.Code == (int)ResultCode.Success)
            {
                btnReject.IsEnabled = false;
                btnCheck.IsEnabled = false;
                DataGridRefresh?.Invoke(this, e);
            }
            ShowAlertHelpter.ShowAlert(result.Msg);
        }

        //审核通过
        private void CheckPass_Click(object sender, RoutedEventArgs e)
        {
            Water_check_history history = new Water_check_history();
            history.WaterPointId = waterpointKEY;
            history.Check_stat = 1;
            var result = waterpointBusiness.Check(history);
            if (result.Code == (int)ResultCode.Success)
            {
                btnReject.IsEnabled = false;
                btnCheck.IsEnabled = false;
                DataGridRefresh?.Invoke(this, e);

                //审核通过的，生成PDF文件 //创建时间：2023-1-12 10:35:36
                //读取文本实体
                var infoView = waterpointBusiness.GetView(editInfo.Id);
                string pdfFileName = $"水露点{editInfo.Report_no}井{editInfo.WellAndWellNo}";
                MakePdfHelpter.CheckPassLoadXps(pdfFileName, "FlowDocumentWaterPoint.xaml", infoView, () =>
                {
                    string dir = DateTime.Now.ToString("yyyy");
                    dir = System.IO.Path.Combine("水露点", dir);
                    return dir;
                });
            }
            ShowAlertHelpter.ShowAlert(result.Msg);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //禁用关闭按钮
            int handle = new WindowInteropHelper(this).Handle.ToInt32();
            WinodowCloseButtonSet.Disable(handle);

            waterpointKEY = editInfo.Id;

            var list = sourceDictBusiness.GetAll();
            list = list.FindAll(g => g.D_stat == 1);

            var userList = userBusiness.GetAllUser();
            userList = userList.FindAll(g => g.U_stat == 1);

            //委托单位  
            cmb_Client.ItemsSource = list.FindAll(x => x.Group_id.Equals("quyang"));
            cmb_Client.DisplayMemberPath = "Item_name";
            cmb_Client.SelectedValuePath = "Id";

            //分析单位  cmb_AnalyUnit 
            cmb_AnalyUnit.ItemsSource = list.FindAll(x => x.Group_id.Equals("quyang"));
            cmb_AnalyUnit.DisplayMemberPath = "Item_name";
            cmb_AnalyUnit.SelectedValuePath = "Id";

            //取样位置
            cmb_TestLocation.ItemsSource = list.FindAll(x => x.Group_id.Equals("quyangLocation"));
            cmb_TestLocation.DisplayMemberPath = "Item_name";
            cmb_TestLocation.SelectedValuePath = "Id";

            //分析人    
            cmb_Analyst.ItemsSource = userList;
            cmb_Analyst.DisplayMemberPath = "User_real_name";
            cmb_Analyst.SelectedValuePath = "Id";

            //编辑赋值
            txt_report_no.Text = editInfo.Report_no;
            cmb_Client.SelectedValue = editInfo.Client;
            cmb_Analyst.SelectedValue = editInfo.Analyst;

            if (editInfo.FluTemperature.HasValue)
            {
                txtFluTemperature.Text = editInfo.FluTemperature.ToMyDecimal2().ToString("f1");
            }            

            txt_well_no.Text = editInfo.WellAndWellNo;
            txt_AirSource.Text = editInfo.AirSource;
            date_Analytime.Text = editInfo.Analytime;
            cmb_AnalyUnit.SelectedValue = editInfo.AnalyUnit;

            //取样位置。修改时间：2023-1-15 21:49:00
            if (System.Text.RegularExpressions.Regex.IsMatch(editInfo.TestLocation, "^\\d*$"))
            {
                cmb_TestLocation.SelectedValue = editInfo.TestLocation;
            }
            else
            {
                txt_TestLocation2.Text = editInfo.TestLocation;
            }

            if (editInfo.AmbientTemperature.HasValue)
            {
                txt_AmbientTemperature.Text = editInfo.AmbientTemperature.ToMyDecimal2().ToString("f1");
            }

            ItemAdd_RefreashListView(this, e);

            //设置所有输入控件只读
            #region 设置所有输入控件只读
            var grid = (DependencyObject)this.Content;
            List<DependencyObject> list2 = new List<DependencyObject>();
            Action<DependencyObject> action = null;
            action = (a) =>
            {
                var count = VisualTreeHelper.GetChildrenCount(a);
                for (int i = 0; i < count; i++)
                {
                    var elem = VisualTreeHelper.GetChild(a, i);
                    list2.Add(elem);
                    action(elem);
                }
            };

            action(grid);

            foreach (var item in list2)
            {
                if (item is TextBox)
                {
                    var elem = (item as TextBox);
                    if (elem.Name.Equals("txtReject"))
                    {
                        continue;
                    }
                    (item as TextBox).IsEnabled = false; //批量设置输入框禁用
                }
                else if (item is ComboBox)
                {
                    (item as ComboBox).IsEnabled = false;
                }
            }

            #endregion

        }

        private void ItemAdd_RefreashListView(object? sender, EventArgs e)
        {
            var list = waterpointBusiness.GetWater_point_itemList(waterpointKEY);
            this.listView.ItemsSource = list;
        }

        //退出
        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void txtReject_GotFocus(object sender, RoutedEventArgs e)
        {
            this.txtReject.Text = "";
        }
    }
}
